﻿using System;

namespace Effects.Effects.Tools
{
    // trinagle-wave oscilator
    /// <summary>
    ///  Implementation of triangle wave oscillator
    /// </summary>
    public class TriangleOscilator : Oscillator
    {
        /// <summary>
        ///  Gets ocillator's value at given time.  Overridden to behave as triangle wave oscillator. 
        /// </summary>
        /// <param name="time"> time of the oscilation </param>
        /// <returns> Value at given time. </returns>
        public override double Amplitude(double time)
        {
            time = time % period;

            if (time <= period / 2)
                return time / (period / 2);
            else
                return (period - time) / (period / 2);
        }

        /// <summary>
        ///  Makes stepTime advance of oscillator's time and returns current value of oscillation.  Overridden to behave as triangle wave oscillator.
        /// </summary>
        /// <param name="stepTime"> Length of a step that oscillator advances in time. </param>
        /// <returns> Current value of an oscillator after the step.</returns>
        public override double Step(double stepTime)
        {
            time += stepTime;
            time %= period;
            if (time <= period / 2)
                return time / (period / 2);
            else
                return (period - time) / (period / 2);
        }

    }
}